// /*
//  * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
//  * Licensed under the Apache License, Version 2.0 (the "License");
//  * you may not use this file except in compliance with the License.
//  * You may obtain a copy of the License at
//  *
//  *     http://www.apache.org/licenses/LICENSE-2.0
//  *
//  * Unless required by applicable law or agreed to in writing, software
//  * distributed under the License is distributed on an "AS IS" BASIS,
//  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  * See the License for the specific language governing permissions and
//  * limitations under the License.
//  */

// #ifndef TETRAHEDRON_H
// #define TETRAHEDRON_H

// #include <GLES3/gl3.h>
// #include <EGL/egl.h>
// #include <EGL/eglext.h>

// #include <string>
// #include <stdint.h>

// #define TRIANGLES_POINT     3
// #define TETRAHEDRON_POINT   12

// class Tetrahedron {
// public:
//     explicit Tetrahedron(std::string& id) : id(id) {};
//     int32_t Init(void* windowHandle, int windowWidth, int windowHeight);
//     void reSizeWindow(int windowWidth, int windowHeight);
//     void Update(float angleXOffset, float angleYOffset);
//     float GetAngleX(void);
//     float GetAngleY(void);
//     int32_t Quit(void);

// public:
//     std::string id;

// private:
//     GLuint LoadShader(GLenum type, const char *shaderSrc);
//     GLuint CreateProgram(const char *vertexShader, const char *fragShader);

//     EGLNativeWindowType mEglWindow;
//     EGLDisplay mEGLDisplay = EGL_NO_DISPLAY;
//     EGLConfig mEGLConfig = nullptr;
//     EGLContext mEGLContext = EGL_NO_CONTEXT;
//     EGLContext mSharedEGLContext = EGL_NO_CONTEXT;
//     EGLSurface mEGLSurface = nullptr;
//     GLuint mProgramHandle;
//     float angleX = 30.0; /* default X angle */
//     float angleY = 45.0; /* default Y angle */
    
//     GLfloat m_widthPercent;
    
//     int m_width;
//     int m_height;

//     GLint mRotationLocation;
//     GLint mTranslationLocation;
//     GLint mMoveOriginLocation;
// };

// #endif /* TETRAHEDRON_H */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 #ifndef TETRAHEDRON_H
 #define TETRAHEDRON_H
 
 #include <vulkan/vulkan.h>
 #include <string>
 #include <vector>
 #include <array>
 #include <stdint.h>
 #include <native_window/external_window.h>
 #include "CreateShaderModule.h"
 
 #define TRIANGLES_POINT     3
 #define TETRAHEDRON_POINT   12
 #define MAX_FRAMES_IN_FLIGHT 2
 
 struct Vertex {
     float pos[3];
     float color[4];
     float normal[3];
 };
 
 struct UniformBufferObject {
     alignas(16) float mx[16];
     alignas(16) float my[16];
     alignas(16) float lightColor[3];
     alignas(16) float lightDirection[3];
 };
 
 class Tetrahedron {
 public:
     explicit Tetrahedron(std::string& id) : id(id) {};
     int32_t Init(void* window, int32_t width, int32_t height);
     void reSizeWindow(int32_t width, int32_t height);
     void Update(float angleXOffset, float angleYOffset);
     float GetAngleX(void);
     float GetAngleY(void);
     int32_t Quit(void);
     void CheckVulkanLibraryStatus();
 
 public:
     std::string id;
 
 private:
     // Vulkan核心对象
     bool mVulkanInitialized = false; 
     VkInstance mInstance = VK_NULL_HANDLE;
     VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
     VkDevice mDevice = VK_NULL_HANDLE;
     VkQueue mGraphicsQueue = VK_NULL_HANDLE;
     VkQueue mPresentQueue = VK_NULL_HANDLE;
     VkSurfaceKHR mSurface = VK_NULL_HANDLE;
     
     // 交换链相关
     VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
     std::vector<VkImage> mSwapChainImages;
     VkFormat mSwapChainImageFormat;
     VkExtent2D mSwapChainExtent;
     std::vector<VkImageView> mSwapChainImageViews;
     std::vector<VkFramebuffer> mSwapChainFramebuffers;
     
     // 渲染通道和管线
     VkRenderPass mRenderPass = VK_NULL_HANDLE;
     VkDescriptorSetLayout mDescriptorSetLayout = VK_NULL_HANDLE;
     VkPipelineLayout mPipelineLayout = VK_NULL_HANDLE;
     VkPipeline mGraphicsPipeline = VK_NULL_HANDLE;
     
     // 命令缓冲区
     VkCommandPool mCommandPool = VK_NULL_HANDLE;
     std::vector<VkCommandBuffer> mCommandBuffers;
     
     // 同步对象
     std::vector<VkSemaphore> mImageAvailableSemaphores;
     std::vector<VkSemaphore> mRenderFinishedSemaphores;
     std::vector<VkFence> mInFlightFences;
     uint32_t mCurrentFrame = 0;
     
     // 缓冲区
     VkBuffer mVertexBuffer = VK_NULL_HANDLE;
     VkDeviceMemory mVertexBufferMemory = VK_NULL_HANDLE;
     std::vector<VkBuffer> mUniformBuffers;
     std::vector<VkDeviceMemory> mUniformBuffersMemory;
     std::vector<void*> mUniformBuffersMapped;
     
     // 描述符
     VkDescriptorPool mDescriptorPool = VK_NULL_HANDLE;
     std::vector<VkDescriptorSet> mDescriptorSets;
     
     // 着色器加载器
     HarmonyShaderLoader mShaderLoader;
     
     // 四面体数据
     float angleX = 30.0f;
     float angleY = 45.0f;
     int32_t m_width;
     int32_t m_height;
     float m_widthPercent;
     std::vector<Vertex> vertices;
     
     // 初始化方法
     int32_t CreateInstance();
     int32_t CreateSurface(void* window);
     int32_t PickPhysicalDevice();
     int32_t CreateLogicalDevice();
     int32_t CreateSwapChain();
     int32_t CreateImageViews();
     int32_t CreateRenderPass();
     int32_t CreateDescriptorSetLayout();
     int32_t LoadShadersFromFile();
     int32_t CreateGraphicsPipeline();
     int32_t CreateFramebuffers();
     int32_t CreateCommandPool();
     int32_t CreateVertexBuffer();
     int32_t CreateUniformBuffers();
     int32_t CreateDescriptorPool();
     int32_t CreateDescriptorSets();
     int32_t CreateCommandBuffers();
     int32_t CreateSyncObjects();
     bool CheckVulkanSupport();
    
     
     // 辅助方法
     uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
     void UpdateUniformBuffer(uint32_t currentImage, float angleX, float angleY);
     void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
     void CleanupSwapChain();
     
     // 队列族查找
     struct QueueFamilyIndices {
         uint32_t graphicsFamily = UINT32_MAX;
         uint32_t presentFamily = UINT32_MAX;
         
         bool isComplete() {
             return graphicsFamily != UINT32_MAX && presentFamily != UINT32_MAX;
         }
     };
     QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
 };
 
 #endif /* TETRAHEDRON_H */